package com.why.day40encryptiondemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {

    private String path = "http://api.m.mtime.cn/Showtime/LocationMovies.api?locationId=";

    //毛泽东 == %E6%AF%9B%E6%B3%BD%E4%B8%9C
    private String url1 = "https://www.baidu.com/s?wd=毛泽东&rsv_spt=1&rsv_iqid=0xfb6dd4de0000105f&issp=1&f=8&rsv_bp=0&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=12&rsv_sug1=18&rsv_sug7=101&rsv_t=31d9XJYnoVZh72wwdpy0JpJSk5eAmFWR1PgvxHPaggFshUpey51Wc8jhOn8nSgDqlISa";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //将地址URLEncoder编码

        String encode = null;
        try {
            encode = URLEncoder.encode("毛泽东", "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Log.d("flag", "--------------------->onCreate: 进行UrlEncode的编码" +encode);

        //使用URLDecoderbianma
        String str = "%E6%AF%9B%E6%B3%BD%E4%B8%9C";
        String decode = null;
        try {
            decode = URLDecoder.decode(str,"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Log.d("flag", "--------------------->onCreate: 使用UrlDecode进行解码" +decode);

        initData();
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                InputStream is = null;
                ByteArrayOutputStream baos = null;
                try {
                    URL url = new URL(url1);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                    conn.setConnectTimeout(3000);
                    if (conn.getResponseCode()==200) {
                        is = conn.getInputStream();
                        baos = new ByteArrayOutputStream();

                        int len = 0;
                        byte[] buf = new byte[1024*8];
                        while ((len=is.read())!=-1){
                            baos.write(buf, 0, len);
                            baos.flush();
                        }
                        byte[] bytes = baos.toByteArray();
                        Log.d("flag", "--------------------->run: bytes:" +new String(bytes));
                    }


                } catch (MalformedURLException e) {
                    e.printStackTrace();
                    Log.d("flag", "--------------------->run: MalformedURLException");
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.d("flag", "--------------------->run: IOException");
                }finally{
                    Log.d("flag", "--------------------->run: finally");
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (baos != null) {
                        try {
                            baos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        });
    }
}
